package com.pengli.mytools.ui;

import android.view.View;
import android.widget.Toast;

import com.pengli.mytools.MyApplication;
import com.pengli.mytools.R;
import com.pengli.mytools.beans.dto.baiduyun.CreateDto;
import com.pengli.mytools.beans.dto.baiduyun.PreCreateDto;
import com.pengli.mytools.beans.vo.baiduyun.CreateVo;
import com.pengli.mytools.beans.vo.baiduyun.PreCreateVo;
import com.pengli.mytools.beans.vo.baiduyun.UploadByPeaceVo;
import com.pengli.mytools.retrofit.apiService.BaiduYunApiService;
import com.pengli.mytools.ui.base.BaseActivity;
import com.pengli.mytools.utils.Md5Util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 配置百度云 获取token
 */
public class BaiduYunUploadActivity extends BaseActivity {


    private MyApplication application;
    private String uploadid;
    private String request_id;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_upload);
        ButterKnife.bind(this);

        application = MyApplication.getInstance();

    }

    @Override
    protected void initData() {



    }

    @Override
    protected void setListener() {

    }

    @OnClick(R.id.start)
    public void onClick(View view){

        String path="/apps/MyTools/apk.apk";


        HashMap<String, String> parm = new HashMap<>();
        parm.put("method", "precreate");
        parm.put("access_token", application.getAccess_token());

        PreCreateDto preCreateDto = new PreCreateDto();
        preCreateDto.setIsdir("0");
        preCreateDto.setAutoinit(1);
        preCreateDto.setPath(path);

        File file = new File(path);
        String fileMD5 = Md5Util.getFileMD5(file);
        ArrayList<String> strings = new ArrayList<>();
        strings.add(fileMD5);
        preCreateDto.setBlock_list(strings);

        BaiduYunApiService baiduYunApiService = application.getBaiduYunApiService();
        Call<PreCreateVo> precreate = baiduYunApiService.precreate(parm, preCreateDto);
        precreate.enqueue(new Callback<PreCreateVo>() {
            @Override
            public void onResponse(Call<PreCreateVo> call, Response<PreCreateVo> response) {
                if (response.isSuccessful()){
                    uploadid = response.body().getUploadid();
                }

            }

            @Override
            public void onFailure(Call<PreCreateVo> call, Throwable t) {

            }
        });

        HashMap<String, String> parm2 = new HashMap<>();
        parm.put("method", "upload");
        parm.put("access_token", application.getAccess_token());
        parm.put("type", "tmpfile");
        parm.put("path", path);
        parm.put("uploadid", uploadid);
        parm.put("partseq", "0");

        File uploadFile=new File(path);
        RequestBody  body=RequestBody.create(MediaType.parse("multipart/form-data"),uploadFile);
        MultipartBody.Part part =MultipartBody.Part.createFormData("file",uploadFile.getName(),body);

        Call<UploadByPeaceVo> uploadByPeaceVoCall = baiduYunApiService.uploadByPeace(parm2, part);
        uploadByPeaceVoCall.enqueue(new Callback<UploadByPeaceVo>() {
            @Override
            public void onResponse(Call<UploadByPeaceVo> call, Response<UploadByPeaceVo> response) {
                if (response.isSuccessful()){
                    request_id = response.body().getRequest_id();
                }

            }

            @Override
            public void onFailure(Call<UploadByPeaceVo> call, Throwable t) {

            }
        });

        HashMap<String, String> parm3 = new HashMap<>();
        parm.put("method", "create");
        parm.put("access_token", application.getAccess_token());

        CreateDto createDto = new CreateDto();
        createDto.setIsdir("0");
        createDto.setSize("");
        createDto.setPath(path);

        Call<CreateVo> createVoCall = baiduYunApiService.create(parm3, createDto);
        createVoCall.enqueue(new Callback<CreateVo>() {

            @Override
            public void onResponse(Call<CreateVo> call, Response<CreateVo> response) {

                if (response.isSuccessful()&&response.body().getErrno()==0){

                    Toast.makeText(BaiduYunUploadActivity.this,"上传成功",Toast.LENGTH_LONG).show();

                }

            }

            @Override
            public void onFailure(Call<CreateVo> call, Throwable t) {

            }
        });

    }
}
